#include <string.h>
#include <stdio.h>
#include "air105.h"

void NVIC_Configuration(void);
void UART_Configuration(void);
void TIMER_Configuration(void);

int32_t TimerFlag = 0;

int main(void)
{
	uint32_t i, j, k, n;

	SYSCTRL_ClocksTypeDef clocks;
	SYSCTRL_PLLConfig(SYSCTRL_PLL_204MHz); // 设置PLL时钟
	SYSCTRL_GetClocksFreq(&clocks);		   // 获取时钟频率

	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_TIMM0 | SYSCTRL_APBPeriph_GPIO, ENABLE);
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_TIMM0, ENABLE);

	UART_Configuration();
	TIMER_Configuration();
	NVIC_Configuration();

	printf("LuatOS AIR105 Timer Demo V1.0.\n");
	TIM_Cmd(TIMM0, TIM_0, ENABLE);

	printf("Test End\n");

	while (1)
	{
	}
}

void TIMER_Configuration(void)
{
	TIM_InitTypeDef TIM_InitStruct;
	TIM_InitStruct.TIM_Period = (SYSCTRL->PCLK_1MS_VAL * 1000);

	TIM_InitStruct.TIMx = TIM_0;
	TIM_Init(TIMM0, &TIM_InitStruct);
	TIM_ITConfig(TIMM0, TIM_InitStruct.TIMx, ENABLE);
}

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_3);

	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannel = TIM0_0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;

	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0 | GPIO_Pin_1, GPIO_Remap_0);

	UART_InitStructure.UART_BaudRate = 115200;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;

	UART_Init(UART0, &UART_InitStructure);
}

int SER_PutChar(int ch)
{
	while (!UART_IsTXEmpty(UART0))
		;
	UART_SendData(UART0, (uint8_t)ch);

	return ch;
}

int fputc(int c, FILE *f)
{
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	if (c == '\n')
	{
		SER_PutChar('\r');
	}
	return (SER_PutChar(c));
}

#ifdef USE_FULL_ASSERT

/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
	printf("Wrong parameters value: file %s on line %d\r\n", file, line);
	/* Infinite loop */
	while (1)
	{
	}
}
#endif
